<template>
  <div>
  <el-container>
    <el-table :data="tableData"  stripe style="width: 100%">
      <el-table-column prop="id" label="ID" width="70px" align="center" sortable></el-table-column>
      <el-table-column prop="author" label="发布作者" align="center"></el-table-column>
      <el-table-column prop="title" label="标题" align="center">
        <template slot-scope="scope">
          <a v-if="scope.row.html !==null" :href="scope.row.html.url" target="_blank" style="text-decoration: none">
            {{scope.row.title}}</a>
          <a v-else target="_blank">{{scope.row.title}}</a>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="类型" align="center" :formatter="typeFormatter"></el-table-column>
      <el-table-column prop="tags" label="标签" align="center"></el-table-column>
      <el-table-column prop="createTime" label="发布日期" align="center" sortable>
        <template slot-scope="scope">
          {{scope.row.createTime | moment("YYYY-MM-DD HH:mm:ss")}}
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="修改日期" align="center" sortable>
        <template slot-scope="scope">
          {{scope.row.updateTime | moment("YYYY-MM-DD HH:mm:ss")}}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            v-if="scope.row.isDelete === 0"
            size="mini"
            type="danger"
            @click="handleAction(scope.row, 'article', 'delete')">删除</el-button>
          <el-button
            v-else
            size="mini"
            type="info"
            @click="handleAction(scope.row, 'article', 'restore')">恢复</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-container>
  <el-row>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="pageTotal"
      :page-size="pageSize"
      :current-page="pageIndex"
      @size-change="handlePageAction"
      @current-change="handlePageAction"
      @prev-text="handlePageAction"
      @next-text="handlePageAction">
    </el-pagination>
  </el-row>
<!--dialog-->
    <el-dialog title="编辑用户" :visible.sync="dialogFormVisible">
      <el-form :model="user">
        <el-form-item label="用户名" >
          <el-input v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" >
          <el-input v-model="user.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机" >
          <el-input v-model="user.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户类型" >
          <el-select v-model="user.type" placeholder="选择用户类型" value="">
            <el-option label="管理员" value="0"></el-option>
            <el-option label="普通用户" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import EventBus from '../EventBus'
export default {
  name: 'Article',
  data () {
    return {
      username: '',
      type: 0,
      email: '',
      phone: '',
      createTime: '',
      tableData: [],
      user: '',
      dialogFormVisible: false,
      pageIndex: 0,
      pageSize: 10,
      pageTotal: 0
    }
  },
  methods: {
    getArticles: function (pageIndex, pageSize) {
      this.axios.get(getGServerUrl() + '/article/find/pages', {
        params: {
          index: pageIndex,
          pageSize: pageSize
        }
      })
        .then(response => {
          console.log(response)
          let data = response.data.data.article
          this.tableData = data.list
          this.pageSize = data.pageSize
          this.pageTotal = data.total
        })
        .catch(error => {
          this.$message({
            message: '请求错误：' + error,
            type: 'error'
          })
        })
    },
    handleEdit: function (index, row) {
      EventBus.$emit('editArticle', row)
    },
    handleAction: function (row, type, action) {
      let msgAction = ''
      let requestUrl = getGServerUrl()
      requestUrl = requestUrl + '/' + type + '/' + action + '/' + row.id
      this.axios.delete(requestUrl).then(response => {
        let count = response.data.data.count
        if (count > 0) {
          if (action === 'delete') {
            row.isDelete = 1
            msgAction = '删除'
          } else if (action === 'restore') {
            row.isDelete = 0
            msgAction = '恢复'
          }
          this.$message({
            message: msgAction + '成功：' + count,
            type: 'success'
          })
        }
      })
        .catch(error => {
          if (action === 'delete') {
            msgAction = '删除'
          } else if (action === 'restore') {
            msgAction = '恢复失败'
          }
          this.$message({
            message: msgAction + '失败: ' + error,
            type: 'error'
          })
        })
    },
    typeFormatter: function (row, column) {
      switch (row.type) {
        case 1:
          return '原创'
        case 2:
          return '转载'
        case 3:
          return '外链'
        case 4:
          return '其他'
        default:
          return '未知类型'
      }
    },
    handlePageAction: function (currentIndex) {
      console.log('current tab:%s current page:%s', this.activeTab, currentIndex)
      this.getArticles(currentIndex, this.pageSize)
      this.pageIndex = currentIndex
    }
  },
  mounted: function () {
    this.getArticles(this.pageIndex, this.pageSize)
  }
}
</script>

<style scoped>
.el-row{
  text-align: center;
  padding: 20px;
}
</style>
